﻿#include "httpschemefactory.h"
#include <fstream>
#include <filesystem>
#include <iostream>

CefRefPtr<CefResourceHandler> HttpSchemeFactory::Create(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, const CefString& scheme_name, CefRefPtr<CefRequest> request)
{
    CEF_REQUIRE_IO_THREAD();
    std::string url = request->GetURL().ToString();
    std::string urlPrefix = "https://afour/";
    url.erase(0, urlPrefix.size());
    size_t paramIndex = url.find_first_of("?");

    if (paramIndex != std::string::npos)
    {
        url.erase(paramIndex);
    }
    TCHAR buffer[MAX_PATH] = { 0 };

    GetModuleFileName(NULL, buffer, MAX_PATH);
    std::filesystem::path targetPath(buffer);
    targetPath = targetPath.parent_path().append("html").append(url);
    if (!std::filesystem::exists(targetPath)) {
        DLOG(INFO) << L"试图加载：" << targetPath.generic_wstring() << L"，但找不到这个文件";
        // 返回404错误页面
        std::string not_found_html = "<html><body><h1>404 Not Found</h1><p>Resource " + url + " not found.</p></body></html>";
        CefRefPtr<CefStreamReader> stream = CefStreamReader::CreateForData(
            static_cast<void*>(const_cast<char*>(not_found_html.c_str())), not_found_html.size());

        return new CefStreamResourceHandler("text/html", stream);
    }
    std::string ext = targetPath.extension().generic_string();
    std::string mime_type_ = ext == ".html" ? "text/html" : "*";
    auto stream = CefStreamReader::CreateForFile(targetPath.generic_wstring());
    return new CefStreamResourceHandler(mime_type_, stream);
}